package edu.ncsu.csc.itrust.beans.loaders;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import edu.ncsu.csc.itrust.beans.InstructionBean;
import edu.ncsu.csc.itrust.beans.OfficeVisitBean;

/**
 * Loads a list of InstructionBeans from a resultset
 * @author Patrick Finegan
 *
 */
public class InstructionBeanLoader implements BeanLoader<InstructionBean> {

	/**
	 * Return a list of InstructionBeans from the resultset
	 * @param rs -- sql result set
	 * @return instruction bean list from rs
	 */
	public List<InstructionBean> loadList(ResultSet rs) throws SQLException {
		List<InstructionBean> list = new ArrayList<InstructionBean>();
		while (rs.next()) {
			list.add(loadSingle(rs));
		}
		return list;
	}

	/**
	 * Populate a single InsructionBean with data from the resultset
	 * @param rs -- sql result set
	 * @return instruction bean from rs
	 */
	public InstructionBean loadSingle(ResultSet rs) throws SQLException {
		InstructionBean ib = new InstructionBean();
		ib.setInstructID(rs.getInt("ID"));
		ib.setHCPID(rs.getLong("MID"));
		ib.setPID(rs.getLong("PID"));
		ib.setDescriptName(rs.getString("descriptName"));
		ib.setURL(rs.getString("url"));
		ib.setComments(rs.getString("comments"));
		ib.setOVID(rs.getLong("ovID"));
		Date d = new Date(rs.getDate("modTime").getTime());
		d.setTime( rs.getTime("modTime").getTime() );
		ib.setModDate(new SimpleDateFormat("MM/dd/yyyy HH:mm").format(d));

		return ib;
	}
	
	public PreparedStatement loadParameters(PreparedStatement ps, InstructionBean ibean) throws SQLException {
		ps.setLong(1, ibean.getHCPID() );
		ps.setLong(2, ibean.getPID() );
		ps.setString(3, ibean.getDescriptName() );
		ps.setString(4, ibean.getURL() );
		ps.setString(5, ibean.getComments() );
		ps.setLong(6, ibean.getOVID() );
		ps.setString(7, new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(new Date() ) );
		return ps;
	}
	
}
